package com.example.demo.msg.socketio;/**
 * Created by kete on 2017-12-19.
 */

import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.annotation.SpringAnnotationScanner;
import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

/**
 * 作者：kete
 * 创建时间：2017-12-19 14:39
 * 功能描述：
 * 版本：
 */
@Configuration
@ConditionalOnProperty(prefix = "guns", name = "web-socket-open", havingValue = "true")
public class SocketIoConfig {
    private static Logger logger = LoggerFactory.getLogger(SocketIoConfig.class);

    /**
     * 正式密码
     */
    public final String keyStorePassword = "zcjb1234";
    /**
     * ssl 证书
     */
    public final String keyStoreStr = "/u3+7QAAAAIAAAABAAAAAQAGdG9tY2F0AAABWtUZo9MAAAUCMIIE/jAOBgorBgEEASoCEQEBBQAEggTqyQ3furuATEgJGGF3CBonpSFENeK03J98q/am1XLYu/lE8uWciq02Inoo/aNd6fmFPLfsbXVStnEQ5+AyWNdKm+48nEc8z8tyrQjnxAetsCbgzQLEe4yVlvTnmPNMu7DZNwX4ozIg/c6eMZEdiYnwR4O6aHk+HgTYFHRFYJntk5f1MYnIgHb8p3eL7/toUet91Zh0BTo9HehlTGh4GemlmtIXC9Ee6AtPkJ7Q+52TzT/EV3/RYdBKbMwMGET4HGKZOZxcqqg/rQsIos3/LDVieKKy/c1QlMDI3W3y6cWr9+ieeFK2OYboQiSeYPTXxPRQGaTH1txCf2rYa76ShOAZH7FB52Ure5Tci/dVwHkOVPN0tBCeqwYjf7VsAVZkiW7stCXIC7MPUZT57s8q4f92ORWgyGGKO1xql6nFb+lI3AP58B81xqYU/12tPFv480MfoazvDQYagr91ywSJhu2lbFmITNu5ap7oGlaOIBvWlSRdPnwHeqRRY20/4pofEF+tlO6gDC/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2TP4mceJhEXRK/1cahw91SC7PUWkXoAISYZXV0UCyEEzuxKwQQ6alngrVlWKaDc+IgsUyDOQrn0XmDZ8onLG5KlpXrTcPrx1/272flbUt3405Bi/naEgYlYQjjaJFkoc=";


    @Value(value = "${websocket.host}")
    private String host;
    @Value(value = "${websocket.port}")
    private String port;

    @Value(value = "${spring.application.env}")
    private String env;


    @Bean
    @ConditionalOnProperty(prefix = "guns", name = "web-socket-open", havingValue = "true")
    public SocketIOServer socketIOServer(){
        logger.info("启动 web socket host=" + host + " port=" + port);
        com.corundumstudio.socketio.Configuration config= new  com.corundumstudio.socketio.Configuration();
        config.setHostname(host);
        config.setPort(new Integer(port));

        try {
            if(!"local".equals(env)){
                Base64 base64 = new Base64();

                InputStream stream = new ByteArrayInputStream(base64.decode(keyStoreStr.getBytes()));
                config.setKeyStorePassword(keyStorePassword);
                config.setKeyStore(stream);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        SocketIOServer socketIOServer = new SocketIOServer(config);
        return socketIOServer;

    }

    @Bean
    @ConditionalOnProperty(prefix = "guns", name = "web-socket-open", havingValue = "true")
    public SpringAnnotationScanner springAnnotationScanner(SocketIOServer socketServer) {
        return new SpringAnnotationScanner(socketServer);
    }


}
